from selenium.webdriver.common.by import By


class PageEcshopFront():
    def __init__(self, driver):
        self.driver = driver
        self.url = "http://192.168.1.241/hdshop/user.php"
        self.time = 20
        # 元素定位器
        self.login_username_loc = (By.NAME, "username")  # 登录界面用户名
        self.login_password_loc = (By.NAME, "password")  # 登录界面密码
        self.login_submit_loc = (By.NAME, "submit")  # 登录界面登录按钮
        self.login_assert_loc = (By.XPATH, '//font[@id="ECS_MEMBERZONE"]/a[2]')  # 登录验证

    def open(self):
        self.driver.get(self.url)
        self.driver.implicitly_wait(self.time)

    def login_username(self, username):
        ''' 用户名输入框 '''
        self.driver.find_element(*self.login_username_loc).clear()
        self.driver.find_element(*self.login_username_loc).send_keys(username)

    def login_password(self, password):
        ''' 密码输入框 '''
        self.driver.find_element(*self.login_password_loc).clear()
        self.driver.find_element(*self.login_password_loc).send_keys(password)

    def login_submit(self):
        ''' 登录按钮 '''
        self.driver.find_element(*self.login_submit_loc).click()

    def login_assert(self):
        actual = self.driver.find_element(*self.login_assert_loc).text
        return actual

    def login(self, username, password):
        self.open()
        self.login_username(username)
        self.login_password(password)
        self.login_submit()
        actual = self.login_assert()
        return actual


class RegisterPage():
    def __init__(self, driver):
        ''' 注册页面类 '''
        self.driver = driver
        self.url = "http://192.168.1.241/hdshop/user.php?act=register"
        self.time = 20
        # 元素定位器
        self.register_username_loc = (By.ID, "username")  # 注册界面用户名
        self.register_email_loc = (By.ID, "email")  # 注册界面email
        self.register_password1_loc = (By.ID, "password1")  # 注册页面密码
        self.register_conform_password_loc = (By.ID, "conform_password")  # 注册页面确认密码
        self.register_qq_loc = (By.XPATH, "//input[@name='extend_field2']")  # 注册页面qq号
        self.register_tel_loc = (By.XPATH, "//input[@name='extend_field3']")  # 注册页面办公电话
        self.register_homephone_loc = (By.XPATH, "//input[@name='extend_field4']")  # 注册页面家庭电话
        self.register_phone_loc = (By.XPATH, "//input[@name='extend_field5']")  # 注册页面手机号
        self.register_submit_loc = (By.XPATH, "//input[@name='Submit']")  # 注册页面提交按钮

        self.register_assert_loc = (By.XPATH, '//font[@id="ECS_MEMBERZONE"]/a[2]')  # 注册验证

    def open(self):
        ''' 打开url '''
        self.driver.get(self.url)
        self.driver.implicitly_wait(self.time)

    def register_username(self, username):
        ''' 用户名输入框 '''
        self.driver.find_element(*self.register_username_loc).clear()
        self.driver.find_element(*self.register_username_loc).send_keys(username)

    def register_email(self, email):
        ''' 用户名输入框 '''
        self.driver.find_element(*self.register_email_loc).clear()
        self.driver.find_element(*self.register_email_loc).send_keys(email)

    def register_password(self, password1):
        ''' 密码输入框 '''
        self.driver.find_element(*self.register_password1_loc).clear()
        self.driver.find_element(*self.register_password1_loc).send_keys(password1)

    def register_conform_password(self, conform_password):
        ''' 密码确认框 '''
        self.driver.find_element(*self.register_conform_password_loc).clear()
        self.driver.find_element(*self.register_conform_password_loc).send_keys(conform_password)

    def register_qq(self, qq):
        ''' qq输入框 '''
        self.driver.find_element(*self.register_qq_loc).clear()
        self.driver.find_element(*self.register_qq_loc).send_keys(qq)

    def register_tel(self, tel):
        ''' 办公电话输入框 '''
        self.driver.find_element(*self.register_tel_loc).clear()
        self.driver.find_element(*self.register_tel_loc).send_keys(tel)

    def register_homephone(self, home_phone):
        ''' 家庭电话输入框 '''
        self.driver.find_element(*self.register_homephone_loc).clear()
        self.driver.find_element(*self.register_homephone_loc).send_keys(home_phone)

    def register_phone(self, phone):
        ''' 手机号输入框 '''
        self.driver.find_element(*self.register_phone_loc).clear()
        self.driver.find_element(*self.register_phone_loc).send_keys(phone)

    def register_submit(self):
        ''' 注册按钮 '''
        self.driver.find_element(*self.register_submit_loc).click()

    def register_assert(self):
        ''' 注册断言 '''
        act = self.driver.find_element(*self.register_assert_loc).text
        return act

    def register(self, username, email, password1, conform_password, phone, qq='', tel='', home_phone=''):
        ''' 注册操作 '''
        self.open()
        self.register_username(username)
        self.register_email(email)
        self.register_password(password1)
        self.register_conform_password(conform_password)
        self.register_qq(qq)
        self.register_tel(tel)
        self.register_homephone(home_phone)
        self.register_phone(phone)
        self.register_submit()
        act = self.register_assert()
        return act
